/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ClassesJava;

import connexion.ConnectionManager;
import interface_graphique.Authentification;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrateur
 */
public class Salle {

    private int num;
    private int Nbplace;
    private String type;
    private String bloc;
    public ConnectionManager c;

    // public Object getbloc;
    public Salle(int num, int Nbplace, String type, String bloc) {
        this.num = num;
        this.Nbplace = Nbplace;
        this.type = type;
        this.bloc = bloc;

    }

    public Salle() {
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getNbplace() {
        return Nbplace;
    }

    public void setNbplace(int Nbplace) {
        this.Nbplace = Nbplace;
    }

    public String gettype() {
        return type;
    }

    public void settype(String type) {
        this.type = type;
    }

    public String getbloc() {
        return bloc;
    }

    public void setbloc(String bloc) {
        this.bloc = bloc;
    }

    public int ajoutersSal(Salle s) {
        int i;
        c = new ConnectionManager();
        String rq = "INSERT INTO  `gestion_des_emplois`.`salle` (num ,type, bloc,Nbplace)"
                + " VALUES (NULL ,'" + s.getNbplace() + "', '" + s.gettype() + "','" + s.getbloc() + "')";
        i = c.setDataBase(rq);
        return i;
    }

    public int modifierSal(Salle s) {
        int i = 0;
        c = new ConnectionManager();
        String rq = "UPDATE  `gestion_des_emplois`.`salle` SET `type` = '" + s.gettype() + "', "
                + "`bloc` = '" + s.getbloc() + "'Nbplace'='" + s.getNbplace() + "' where id=" + s.getNum();
        i = c.setDataBase(rq);
        return i;

    }

    public int supprimerSal(Salle s) {
        int i = 0;
        c = new ConnectionManager();
        String rq = "DELETE FROM `gestion_des_emplois`.`salle` where id=" + s.getNum();
        i = c.setDataBase(rq);
        return i;
    }

    public Vector<Salle> getSalle() {
        Vector<Salle> res = new Vector();
        try {
            c = new ConnectionManager();
            ResultSet rs = c.getDataBase("select * from salle");

            while (rs.next()) {
                Salle s = new Salle(rs.getInt(1), rs.getInt(4), rs.getString(2), rs.getString(3));
                res.add(s);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Salle.class.getName()).log(Level.SEVERE, null, ex);
        }

        return res;

    }

    public Vector<Salle> getSalleByZone(int col, int row) {
        Vector<Salle> res = new Vector();
        try {
            c = new ConnectionManager();
            ResultSet rs = c.getDataBase("select * from salle where num not in (SELECT id_s FROM cours "
                    + "WHERE row=" + row + " and col=" + col + " and id_s is not null)");

            while (rs.next()) {
                Salle s = new Salle(rs.getInt(1), rs.getInt(4), rs.getString(2), rs.getString(3));
                res.add(s);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Salle.class.getName()).log(Level.SEVERE, null, ex);
        }

        return res;

    }

    public Salle getSalleById(int id) {
        Salle s = null;
        try {
            c = new ConnectionManager();
            ResultSet rs = c.getDataBase("select * from salle where num =" + id);

            while (rs.next()) {
                s = new Salle(rs.getInt(1), rs.getInt(4), rs.getString(2), rs.getString(3));
            }

        } catch (SQLException ex) {
            Logger.getLogger(Salle.class.getName()).log(Level.SEVERE, null, ex);
        }

        return s;

    }
}
